package uni.call.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class LogDBAdapter 
{
	private static final String DATABASE_NAME = "Log.db";
	private static final int    DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "Log";
	
    public static final String KEY_ID = "_id";
    public static final String KEY_Type = "type";
    public static final String KEY_Name = "name";
    public static final String KEY_Peer = "peer";
    public static final String KEY_Duration = "duration";
    public static final String KEY_DATE = "date";
    
	private static final String CREATE_TABLE_Log = 
		"create table " + TABLE_NAME
	+ " (" + KEY_ID + " integer primary key autoincrement, " 
	+ KEY_Type
	+ " text not null, " 
	+ KEY_Name
	+ " text not null, " 
	+ KEY_Peer
	+ " text not null, " 
	+ KEY_Duration
	+ " text not null, " 
	+ KEY_DATE 
	+ " text not null);";
	
	private SQLiteDatabase db;
	private LogDBOpenHelper dbHelper;

	private Context context;
	
    public LogDBAdapter(Context c)
    {
    	context = c;
    }

    public LogDBAdapter OpenToRead() throws android.database.SQLException 
    {
    	dbHelper = new LogDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    	db = dbHelper.getReadableDatabase();
    	return this;
    }
    
    public LogDBAdapter OpenToWrite() throws android.database.SQLException
    {
    	dbHelper = new LogDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    	db = dbHelper.getWritableDatabase();
    	return this;	
    }

    public void Close() 
    {
    	dbHelper.close();
    }
    
    public long Insert(String type, String name, String peer, String duration, String time)
    {
    	ContentValues values = new ContentValues();
    	values.put(KEY_Type, type);
    	values.put(KEY_Name, name);
    	values.put(KEY_Peer, peer);
    	values.put(KEY_Duration, duration);
    	values.put(KEY_DATE, time);
    	System.out.print(values.toString());
    	return db.insert(TABLE_NAME, null, values);
    }
    
    public int DeleteAll()
    {
    	return db.delete(TABLE_NAME, null, null);	
    }
    
    public Cursor GetAll()
    {
    	String[] columns = new String[]{KEY_ID, KEY_Type,KEY_Name,KEY_Peer,KEY_Duration,KEY_DATE};
    	Cursor cursor = db.query(TABLE_NAME, columns,
    	  null, null, null, null, KEY_ID + " DESC");

    	return cursor;
    }
    
	private static class LogDBOpenHelper extends SQLiteOpenHelper
	{
		public LogDBOpenHelper(Context context, String name,CursorFactory factory, int version)
		{
				super(context, name, factory, version);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(CREATE_TABLE_Log);
		}
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
		{
		}
	}
}
